package com.isunland.managesystem.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.utils.MyUtils;

public class ShareToFriendsFragment extends Fragment implements OnClickListener {

  private Context mContext;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity mActivity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      if (NavUtils.getParentActivityName(mActivity) != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
      }
      actionBar.setTitle(R.string.share);
    }
    mContext = getActivity();

    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_shareto_friends, container, false);

    Button mBtShareToFriendSetting = (Button) view.findViewById(R.id.bt_shareToFriend_setting);
    mBtShareToFriendSetting.setOnClickListener(this);
    ImageView ivQrCode = (ImageView) view.findViewById(R.id.iv_qrCode);
    ivQrCode.setImageResource(
        MyUtils.isWeiJia() ? R.drawable.qrcode_weijia : R.drawable.qrcode_guoxin);
    return view;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onClick(View v) {
    String url = mContext.getString(R.string.welcomeToJoin, getString(R.string.app_name)) + (
        MyUtils.isWeiJia() ? ApiConst.URL_DOWNLOAD_LINK_WEIJIA : ApiConst.URL_DOWNLOAD_LINK_GUOXIN);
    String title = mContext.getString(R.string.shareUrl);
    switch (v.getId()) {
      case R.id.bt_shareToFriend_setting:

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        intent.putExtra(Intent.EXTRA_TEXT, url);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, title));
        break;

      default:
        break;
    }
  }
}
